<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */

namespace MoChat\App\Call\Logic;

use Hyperf\Di\Annotation\Inject;
use MoChat\App\User\Contract\UserContract;

/**
 * 呼叫记录.
 *
 * Class IndexLogic
 */
class RecordIndexLogic
{
    /**
     * @Inject
     * @var UserContract
     */
    protected $userService;

    /**
     * 呼叫记录列表.
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function handle(array $params): array
    {
        //返回的数据格式
        $data = [
            'page' => [
                'page' => $params['page'],
                'perPage' => $params['perPage'],
                'total' => 10,
                'totalPage' => 1,
            ],
            'list' => [],
        ];
        //请求外部接口
        $token = send_http_request(env("CALL_CENTER_BASE_URL"), '/api/login', 'GET', [
            'headers' => [
                'Content-Type' => 'application/json',
            ],
            'verify' => false,
            'query' => [
                'username' => env("CALL_CENTER_USERNAME"),
                'password' => env("CALL_CENTER_PASSWORD"),
            ]
        ]);

        $response = send_http_request(env("CALL_CENTER_BASE_URL"), '/api/ola/call_log', 'GET', [
            'headers' => [
                'X-Auth-Token' => $token,
                'Content-Type' => 'application/json',
            ],
            'verify' => false,
            'query' => [
                'call_type' => $params['callType'],
                'callerNumber' => $params['callerNumber'],
                'destNumber' => $params['destNumber'],
                'startTime' => $params['startTime'],
                'endTime' => $params['endTime'],
            ]
        ]);

        
        $data['list'] = my_array_change_key($response);
        // $data['page']['total'] = $res['total'];
        // $data['page']['totalPage'] = $res['last_page'];
        return $data;
    }
}
